package com.kinglone.back.user.controller;

import com.kinglone.back.user.model.User;
import com.kinglone.back.user.service.UserService;
import com.kinglone.common.entity.PageBean;
import com.kinglone.common.utils.ResponseUtil;
import com.kinglone.common.utils.ResultMsg;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    protected final static Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/showUser")
    public String showUser(HttpServletRequest request){
        logger.info("跳转到菜单列表");
        return "user/userList";
    }
    @RequestMapping("/roleAssign/{userId}")
    public String roleAssign(@PathVariable Integer userId, Model model){
        logger.info("跳转到角色分配页面");
        //校验userId是否为空，抛出自定义异常

        model.addAttribute("userId", userId);
        return "user/roleassign";
    }

    @RequestMapping("/userList")
    public String userList(String page, String limit, User user, HttpServletResponse response){
        logger.info("----------------------查询用户列表信息");
        try {
            PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(limit));
            JSONObject result = new JSONObject();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("param",user);
            map.put("start", pageBean.getStart());
            map.put("size", pageBean.getPageSize());

            List<User> userlist = userService.list(map);
            int count = userService.getTotal(map);
            JSONArray jsonArray = JSONArray.fromObject(userlist);
            result.put("code",0);
            result.put("msg","");
            result.put("data", jsonArray);
            result.put("count", count);
            ResponseUtil.write(response, result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分配角色
     */
    @RequestMapping("/setRole")
    @ResponseBody
    public ResultMsg setRole(@RequestParam("userId") Integer userId, @RequestParam("roleIds") String roleIds){
        ResultMsg msg = new ResultMsg();
        try {
            //校验userId 和 roleIds
            //不能修改超级管理员
            //判断当前登录的用户是否有操作这个用户的权限

            userService.setRoles(userId, roleIds);
            msg.setMessage("角色分配成功!");
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("角色分配失败!");
            return msg;
        }
        return msg;
    }

}
